<template>
  <div id="jobMTopSearch">
    <h1 class="con-right-title">
      作业发布
    </h1>
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="是否推荐" prop="isRecommend">
          <el-select v-model="formInline.isRecommend" clearable>
            <el-option
              v-for="item in recommendList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择学院" prop="categoryId">
          <el-select v-model="formInline.categoryId" clearable>
            <el-option
              v-for="item in collegeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作业类型" prop="jobType">
          <el-select v-model="formInline.jobType" clearable>
            <el-option
              v-for="item in taskList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否置顶" prop="isTop">
          <el-select v-model="formInline.isTop" clearable>
            <el-option
              v-for="item in topList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序规则" prop="order">
          <el-select v-model="formInline.order">
            <el-option
              v-for="item in sortList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作业打分" prop="level">
          <el-select v-model="formInline.level" clearable>
            <el-option
              v-for="item in gradeList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除状态" prop="isDel">
          <el-select v-model="formInline.isDel">
            <el-option
              v-for="item in delStatusList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程类型" prop="courseType">
          <el-select v-model="formInline.courseType" clearable>
            <el-option
              v-for="item in courseList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程等级" prop="courseLevel">
          <el-select v-model="formInline.courseLevel" clearable>
            <el-option
              v-for="item in courseLevelList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人审结果" prop="checkStatu">
          <el-select v-model="formInline.checkStatu" clearable>
            <el-option
              v-for="item in pExamineList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机审结果" prop="machineStatu">
          <el-select v-model="formInline.machineStatu" clearable>
            <el-option
              v-for="item in mExamineList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作业码" prop="identCode">
          <el-input
            v-model="formInline.identCode"
            placeholder="请根据作业码搜索"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="作业名称" prop="taskTitle">
          <el-input
            v-model="formInline.taskTitle"
            placeholder="请根据作业名称搜索"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="讲师姓名" prop="teacherName">
          <el-input
            v-model="formInline.teacherName"
            placeholder="请根据讲师姓名搜索"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="时间规则" prop="timeType">
          <el-select v-model="formInline.timeType" placeholder="">
            <el-option
              v-for="item in timeOrderList"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
export default {
  name: 'JobMTopSearch',
  msg: '作业管理-我的发布-顶部搜索',
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          isRecommend: '', // 是否推荐
          categoryId: '', // 选择学院
          jobType: '', // 作业类型
          isTop: '', // 是否置顶
          order: '', // 排序规则
          level: '', // 作业打分
          isDel: '', // 删除状态
          courseType: '', // 课程类型
          courseLevel: '', // 课程等级
          checkStatu: '', // 人审结果
          machineStatu: '', // 机审结果
          identCode: '', // 作业码
          taskTitle: '', // 作业名称
          teacherName: '', // 讲师姓名
          timeType: '', // 时间规则
          startTime: '', // 开始时间
          endTime: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      allRecommendList: [{ dictLabel: '全部', dictValue: '' }], // 是否推荐全部列表
      recommendList: [], // 是否推荐展示列表
      allCollegeList: [{ dictLabel: '全部学院', dictValue: '' }], // 全部学院列表
      collegeList: [], // 学院展示列表
      taskList: [{ dictLabel: '全部', dictValue: '' }], // 作业类型
      topList: [{ dictLabel: '全部', dictValue: '' }], // 是否置顶
      sortList: [], // 排序规则
      gradeList: [{ dictLabel: '全部', dictValue: '' }], // 作业打分
      delStatusList: [], // 删除状态
      courseList: [{ dictLabel: '全部', dictValue: '' }], // 课程类型
      courseLevelList: [{ dictLabel: '全部', dictValue: '' }], // 课程等级
      timeOrderList: [], // 时间规则
      pExamineList: [
        // 人审结果
        { dictLabel: '全部', dictValue: '' }
      ],
      mExamineList: [
        // 机审结果
        { dictLabel: '全部', dictValue: '' }
      ],
      rules: {
        isRecommend: {},
        categoryId: {},
        jobType: {},
        isTop: {},
        order: {},
        level: {},
        isDel: {},
        overdue: {},
        courseType: {},
        courseLevel: {},
        identCode: {},
        taskTitle: {},
        teacherName: {},
        timeType: {},
        checkStatu: {},
        machineStatu: {},
        createTime: { type: 'array' }
      },
      artList: [],
      notArtList: [],
      drowList: [],
      notDrowList: [],
      artValue: 'j5m48deg',
      drowValue: 'BOARDPAINTING', //板绘之星
      idearValue: 'CREATIVE' //创意之星
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startTime = newVal[0]
          this.formInline.endTime = newVal[1]
        } else {
          this.formInline.startTime = ''
          this.formInline.endTime = ''
        }
      },
      deep: true
    },
    'formInline.isRecommend': {
      //监听是否推荐
      handler(newVal, oldVal) {
        if (newVal === 'BOARDPAINTING') {
          //板绘之星,只要美术学院
          this.collegeList = [...this.artList]
          this.formInline.categoryId = this.artValue
          return
        }
        if (newVal === 'CREATIVE') {
          //创意之星，不要美术学院
          this.collegeList = [...this.notArtList]
          if (this.formInline.categoryId === this.artValue) {
            this.formInline.categoryId = ''
          }
          return
        }
        this.collegeList = [...this.allCollegeList]
      },
      deep: true
    },
    'formInline.categoryId': {
      //监听学院
      handler(newVal, oldVal) {
        if (newVal === '') {
          //全部学院,显示全部列表
          this.recommendList = [...this.allRecommendList]
          return
        }
        if (newVal === this.artValue) {
          //美术学院,显示有板绘之星列表
          this.recommendList = [...this.drowList]
          return
        }
        //美术、全部，以外的其他学院，显示无板绘之星列表
        this.recommendList = [...this.notDrowList]
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      //设置默认数据
      this.setDefaultValue()
    },
    /* 获取数据状态 */
    async getStatusList() {
      // 是否推荐
      let data1 = await sysNormalDisable(
        'task_whether_recommend',
        'recommendType'
      )
      this.allRecommendList = this.allRecommendList.concat(data1)
      this.recommendList = [...this.allRecommendList]
      this.drowList = this.getRecommend(true)
      this.notDrowList = this.getRecommend(false)
      // 学院
      let data2 = await sysNormalDisable('sys_college', 'collegeType')
      this.allCollegeList = this.allCollegeList.concat(data2)
      this.collegeList = [...this.allCollegeList]
      this.artList = this.getShowCollege(true)
      this.notArtList = this.getShowCollege(false)
      // 是否置顶
      let data3 = await sysNormalDisable('sys_is_top')
      this.topList = this.topList.concat(data3)
      // 作业打分
      let data4 = await sysNormalDisable('sys_job_level', 'jobLevel')
      this.gradeList = this.gradeList.concat(data4)
      // 删除状态
      this.delStatusList = await sysNormalDisable('sys_is_delete')
      // 课程类型
      let data7 = await sysNormalDisable('sys_course_type')
      this.courseList = this.courseList.concat(data7)
      // 课程等级
      let data8 = await sysNormalDisable('sys_course_level')
      this.courseLevelList = this.courseLevelList.concat(data8)
      // 作业类型
      let data10 = await sysNormalDisable('sys_task_type')
      this.taskList = this.taskList.concat(data10)
      // 排序规则
      let data11 = await sysNormalDisable('sys_home_order_type')
      this.sortList = this.sortList.concat(data11)
      // 时间规则
      let data13 = await sysNormalDisable('sys_home_time_type')
      this.timeOrderList = [...data13]
      // 人审结果
      let data14 = await sysNormalDisable('sys_trial_status')
      this.pExamineList = this.pExamineList.concat(data14)
      // 机审结果
      let data15 = await sysNormalDisable('sys_machine_statu')
      this.mExamineList = this.mExamineList.concat(data15)
      //设置默认数据
      this.setDefaultValue()
      //触发获取列表
      this.$emit('toSearch')
    },
    /*获取展示学院信息*/
    getShowCollege(flag) {
      let list = [...this.allCollegeList]
      let index = list.findIndex((item) => item.dictValue === this.artValue)
      if (flag) {
        //只要美术学院
        return [list[index]]
      } else {
        //不要美术学院
        list.splice(index, 1)
        return list
      }
    },
    /*获取展示是否推荐列表*/
    getRecommend(flag) {
      //flag: true,要有板绘之星，无创意之星
      //flag: false,要有创意之星，无板绘之星
      let list = [...this.allRecommendList]
      let value = flag ? this.idearValue : this.drowValue
      let index = list.findIndex((item) => item.dictValue === value)
      list.splice(index, 1)
      return list
    },
    /*设置默认数据*/
    setDefaultValue() {
      this.formInline.order = this.sortList[0].dictValue
      this.formInline.isDel = this.delStatusList[0].dictValue
      this.formInline.timeType = this.timeOrderList[0].dictValue
    }
  }
}
</script>

<style lang="less">
#jobMTopSearch {
  .el-input {
    &.el-input--suffix {
      width: 182px !important;
    }
  }

  label.el-form-item__label {
    width: 70px !important;
  }
}
</style>
